package org.xu.logic;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.core.utils.HttpFormUtil;
import com.core.utils.UUIDGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xu.entity.WorkFlowLogEntity;
import org.xu.enums.WorkTypeEnum;
import org.xu.service.WorkFlowLogService;

import java.util.Date;

@Slf4j
@Service
public class FeedAlmondLogicImpl implements FeedAlmondLogic{

    @Autowired
    private WorkFlowLogService workFlowLogService;
    private static final String dataIdStr = "daka";

    @Override
    public void feedAlmond(String name,String token) {
        StringBuffer sbf = new StringBuffer();
        sbf.append("------WebKitFormBoundaryBxDBT2MDYihxEwK1\r\n")
                .append("Content-Disposition: form-data; name=\"token\"\r\n")
                .append("\r\n")
                .append(token.trim())
                .append("\r\n------WebKitFormBoundaryBxDBT2MDYihxEwK1--");
    //    System.out.println(sbf.toString());
        String urlString = "https://h5up.bluebirdh5.top/sq/YxDecennial/myAlmond";
        String str = HttpFormUtil.formCommit("response",urlString, sbf.toString(), "----WebKitFormBoundaryBxDBT2MDYihxEwK1");
      //  System.out.println(str);
       //  String str = "{\"code\":200,\"data\":{\"gold_num\":700,\"feed_almond\":[{\"id\":26894,\"almond_id\":1,\"form\":3,\"feed_num\":7,\"total\":9,\"sort\":1}],\"send_id\":0,\"daily_food_id\":[],\"sign_food_id\":[],\"food_num\":32},\"msg\":\"获取成功\"}";
        StringBuilder stringBuilder = new StringBuilder();
        try {
            JSONObject jsonObject = JSONObject.parseObject(str);
            Object code = jsonObject.get("code");
            if (code instanceof Integer && new Integer(200).equals(code)){
                jsonObject = (JSONObject)jsonObject.get("data");
                Integer gold_num = (Integer)jsonObject.get("gold_num");
                System.out.println(gold_num);
                stringBuilder.append("用户:"+ name +",总金币:"+ gold_num +",");
                JSONArray jsonArray = (JSONArray)jsonObject.get("feed_almond");
                // 遍历 JSONArray 并访问每个 JSONObject 的属性
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject2 = jsonArray.getJSONObject(i);
                    int total = jsonObject2.getInteger("total");
                    int feed_num = jsonObject2.getInteger("feed_num");
                    int sort = jsonObject2.getInteger("sort");
                  //  System.out.println("total:" + total + ", feed_num: " + feed_num +",sort:" + sort);
                    stringBuilder.append("【序号:"+ sort + ",需喂总数:" + total + ",已喂: " + feed_num +"次】");
                }
            }
        }catch (Exception e){
            stringBuilder.append(str);
        }
     //   System.out.println("------------------");
     //   System.out.println(response);
        log.info("test_" + name +",{}",stringBuilder.toString());
        WorkFlowLogEntity entity = new WorkFlowLogEntity();
        entity.setId(UUIDGenerator.generate());
        entity.setDataId(dataIdStr);
        entity.setApplyNo("test_"+ name);
        entity.setCreateTime(new Date());
        entity.setOperationResult(name +":机器类别获取完成");
        entity.setRemark(stringBuilder.toString());
        entity.setDeleted(false);
        entity.setWorkType(WorkTypeEnum.SIGN);
        workFlowLogService.insert(entity);
    }

    public static void main(String[] args) {

        String str = "{\"code\":200,\"data\":{\"gold_num\":700,\"feed_almond\":[{\"id\":26894,\"almond_id\":1,\"form\":3,\"feed_num\":7,\"total\":9,\"sort\":1}],\"send_id\":0,\"daily_food_id\":[],\"sign_food_id\":[],\"food_num\":32},\"msg\":\"获取成功\"}";
        JSONObject jsonObject = JSONObject.parseObject(str);
        Object code = jsonObject.get("code");
        if (code instanceof Integer && new Integer(200).equals(code)){
            jsonObject = (JSONObject)jsonObject.get("data");
            Integer gold_num = (Integer)jsonObject.get("gold_num");
            System.out.println(gold_num);
            JSONArray jsonArray = (JSONArray)jsonObject.get("feed_almond");
            // 遍历 JSONArray 并访问每个 JSONObject 的属性
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject2 = jsonArray.getJSONObject(i);
                int total = jsonObject2.getInteger("total");
                int feed_num = jsonObject2.getInteger("feed_num");
                int sort = jsonObject2.getInteger("sort");
                System.out.println("total" + total + ", feed_num: " + feed_num +",sort:" + sort);
            }
        }else {

        }
    }



}
